package com.md.youyouhui.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.NotificationCompat;


import com.md.youyouhui.BuildConfig;
import com.md.youyouhui.R;
import com.tencent.bugly.beta.download.DownloadListener;
import com.tencent.bugly.beta.download.DownloadTask;

import java.lang.ref.WeakReference;
import java.util.Arrays;

import timber.log.Timber;

/**
 * @author yawei
 * @data on 2018/9/14 下午1:15
 * @email zyawei@live.com
 */
public class DownLoadNotification implements DownloadListener {

    private static final int NOTIFY_ID = 0x99;
    private Context context;
    private WeakReference<NotificationManager> weakReference = null;
    public DownLoadNotification(Context context) {
        this.context = context;
    }

    private NotificationManager notificationManager() {
        if (null == weakReference || null == weakReference.get()) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            weakReference = new WeakReference<>(notificationManager);
        }
        return weakReference.get();
    }

    private void showProgress(int progress) {
        Timber.i("showProgress %d ", progress);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = notificationManager().getNotificationChannel(BuildConfig.APPLICATION_ID);
            if (null == notificationChannel) {
                NotificationChannel channelDownload = new NotificationChannel(BuildConfig.APPLICATION_ID + ".download", "下载", NotificationManager.IMPORTANCE_HIGH);
                NotificationChannel channelDefault = new NotificationChannel(BuildConfig.APPLICATION_ID + ".default", "默认", NotificationManager.IMPORTANCE_DEFAULT);
                notificationManager().createNotificationChannels(Arrays.asList(channelDownload, channelDefault));
            }
        }
        Notification notification = new NotificationCompat.Builder(context, BuildConfig.APPLICATION_ID + ".download")
                .setContentTitle("正在下载")
                .setContentText(progress + "%")
                .setTicker("开始下载")
                .setProgress(100, progress, false)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setOngoing(true)
                .build();
        notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
        notificationManager().notify(NOTIFY_ID, notification);
    }


    @Override
    public void onReceive(DownloadTask downloadTask) {
        long save = downloadTask.getSavedLength();
        long total = downloadTask.getTotalLength();
        showProgress((int) (save * 100 / total));
    }

    @Override
    public void onCompleted(DownloadTask downloadTask) {
        notificationManager().cancel(NOTIFY_ID);
    }

    @Override
    public void onFailed(DownloadTask downloadTask, int i, String s) {
        notificationManager().cancel(NOTIFY_ID);
    }
}
